import { h, reactive, ref } from 'vue'
import {  refs, } from 'dsrz/utils'
import { useMessageBox } from 'dsrz/hooks/useMessageBox'
import VisitorText from '@/components/visitor/text/index.vue'
// elMessageBox, message,

// import VisitorText from '@/components/visitor/text/index.vue'
// import {getClassList, getStudentDetail, getStudentList, studentTemplateExport, syncStudent} from '@/api/staffManagement'
import { useResetReactive } from 'dsrz/hooks/useReset'
// import { getGradeList, resetPwd } from '@/api/userManagement'
import router from '@/router'
// import HomeWebText from '@/components/homeweb/text/index.vue'
import { studentStatus } from '@/enum/index'

import type {
    BtnsProps,
    ColumnsProps,
    ItemsProps,
    KeysProps,
} from './types/index'
import {ElMessage} from "element-plus";

const classOptions = ref([])
const getClassOptions = async (params = {}) => {
//   const res = await getClassList(params)
//   classOptions.value = res.data
}
getClassOptions()
export const reviseClassOptions = (data = {}) => {
  params.classId = ''
  getClassOptions(data)
}
const {
  params,
  exposes: { query, reset },
} = useResetReactive({})

const activeName = ref('1')
const editActiveName = (tabVal: string) => {
  activeName.value = tabVal
  params.enable = Boolean(+tabVal)
}
const items = reactive<ItemsProps[]>([
    {
        prop: 'name',
        is: 'currency-input',
        isEncrypt:true,
        bind: {
          placeholder: '姓名、学号、手机号',
          clearable: true,
        }
    },
    {
        prop: 'hasAuth',
        is: 'el-select-v2',
        bind: {
          options: [
            {
              value: 1,
              label: '已实名认证',
            },
            {
              value: 0,
              label: '未实名认证',
            }
          ],
          placeholder: '实名认证',
          filterable: true,
          clearable: true,
        }
    },
    {
      prop: 'classId',
      is: 'el-select-v2',
      bind: {
        options: classOptions,
        placeholder: '班级',
        clearable: true,
        filterable: true,
        props: {
          label: 'name',
          value: 'id'
        }
      }
    },
    {
      prop: 'practice',
      is: 'el-select-v2',
      bind: {
          options: [
              {
                value: 1,
                label: '是',
              },
              {
                value: 0,
                label: '否',
              }
          ],
        placeholder: '是否校外实习',
        clearable: true,
      }
    },
    {
      prop: 'status',
      is: 'el-select-v2',
      bind: {
        options:studentStatus,
        placeholder: '学籍状态',
        clearable: true,
        filterable: true,
      }
    },
    {
      is: 'currency-button',
      bind: {
        text: true,
        title: '清空',
        icon: 'DsrzClear',
      },
      on: {
        click: () => {
          const grandeIds = params.grandeIds
          const type = params.deptType
          const id = params.deptId
          reset()
          params.enable = Boolean(+activeName.value)
          params.grandeIds = grandeIds
          if(type != null) params.deptType = type
          if(id != null) params.deptId = id
        },
      },
    }
])

const btns = reactive<BtnsProps[]>([
    {
      is: 'currency-button',
      display: () => activeName.value === '1',
      bind: {
        type: 'primary',
        text: true,
        title: '学生数据导入',
        icon: 'DsrzExport',
      },
      on: {
          click: async() => {
            // const res = await getGradeList({ enable: 1 })
            // refs.studentDataImportRef.getParams({
            //   data: res.data
            // })
            refs.studentDataImportRef.getParams({
              data: [
                {name: '2026级', id: 6},
                {name: '2025级', id: 5},
                {name: '2024级', id: 4},
              ],
              title: '学生数据导入',
            })
          }
      },
    },
    {
        is: 'currency-button',
        display: () => activeName.value === '1',
        bind: {
          type: 'primary',
          text: true,
          title: '下载导入模板',
          icon: 'DsrzExport',
        },
        on: {
          click: () => {
            // studentTemplateExport({}, {
            //   responseType: 'blob'
            // }).then(res => {
            //   const blob = new Blob([res.data], {
            //     type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
            //   });
            //   const downloadUrl = window.URL.createObjectURL(blob);
            //   const link = document.createElement('a');
            //   link.href = downloadUrl;
            //   link.download = '导入学生模板.xlsx'; // 指定下载文件名 22
            //   document.body.appendChild(link);
            //   link.click();
            //   document.body.removeChild(link);
            //   window.URL.revokeObjectURL(downloadUrl);
            // }).catch(err => {
            //   console.log(err, '---err');
            // });
          }
        },
    },
    {
      is: 'currency-button',
      display: () => activeName.value === '1',
      bind: {
        type: 'primary',
        text: true,
        title: '批量修改',
        icon: '',
      },
      on: {
          click: async() => {
            // const res = await getGradeList({ enable: 1 })
            // refs.exportAndDownRef.getParams({
            //   data: res.data,
            //   title: '批量修改'
            // })
            refs.studentDataImportRef.getParams({
              data: [
                {name: '2026级', id: 6},
                {name: '2025级', id: 5},
                {name: '2024级', id: 4},
              ],
              title: '批量修改',
            })
          }
      },
    },
    {
      is: 'currency-button',
      display: () => activeName.value === '1',
      bind: {
        type: 'primary',
        title: '新增',
        icon: 'DsrzAdd',
      },
      on: {
          click: () => {
            console.log('== 新增 ')
            // return
            router.push({
              name: 'addStudentPage',
              state: {
                params: {
                  action: 'add',
                  title: '新增学生信息',
                  content: ''
                }
              }
            })
          }
      },
    },
    {
      is: 'currency-button',
      display: () => activeName.value === '1',
      bind: {
        type: 'primary',
        title: '导出',
        icon: 'DsrzExport',
      },
      on: {
          click: () => {
            console.log('== 导出 ')
          }
      },
    }
])

const columns = reactive<ColumnsProps[]>([
    { type: 'index', label: '序号', width: 60, align: 'center' },
    { prop: 'nickname', label: '姓名', is: h('CurrencyText', { label: '姓名' }),  isDecrypt: true, align: 'center' },
    // is: h(HomeWebText),
    {
      prop: 'hasAuth', label: '实名认证',  bind: {
          showType: 2,
          enumName: 'hasAuthList'
      },
      width: 100
    },
    // is: h(VisitorText, { label: '学号' }),
    { prop: 'username', label: '学号', is: h(VisitorText, { label: '学号' }), isDecrypt: true, width: 200, align: 'center', bind: { color: '#178782' } },
    // is: h(VisitorText, { label: '手机号' }),
    { prop: 'phone', label: '手机号',  isDecrypt: true, width: 150, showOverflowTooltip: true, align: 'center' },
    {
        // is: h(HomeWebText),
      prop: 'sex', label: '性别',  bind: {
        showType: 2,
        enumName: 'sexData'
      }
    },
    { prop: 'deptName', label: '二级学院' },
    { prop: 'gradeName', label: '年级' },
    { prop: 'className', label: '班级', showOverflowTooltip: true },
    { prop: 'headTeacherName', label: '班主任', isDecrypt: true },
    {
        // is: h(HomeWebText),
      prop: 'enable', label: '账号状态',  bind: {
        showType: 2,
        enumName: 'enableStatus'
      }
    },
    {
        // is: h(HomeWebText),
      prop: 'status', label: '学籍状态',  bind: {
        showType: 2,
        enumName: 'statusMap'
      }
    },

    { prop: 'birthDate', label: '出生年月日', width: 120 },
    { prop: 'buildName', label: '住宿信息', showOverflowTooltip: true },
    {
        // is: h(HomeWebText),
      prop: 'practice', label: '是否校外实习',  bind: {
        showType: 2,
        enumName: 'practiceData'
      }
    },
    { prop: 'nation', label: '民族' },
    {
        // is: h(HomeWebText),
      prop: 'polityIdentity', label: '政治面貌',  bind: {
        showType: 2,
        enumName: 'polityIdentity'
      }
    },
    { prop: 'belief', label: '宗教信仰' },
    // is: h(VisitorText, { label: '父亲联系方式' }),
    { prop: 'fatherTel', label: '父亲联系方式',  isDecrypt: true, width: 130},
    // is: h(VisitorText, { label: '母亲联系方式' }),
    { prop: 'motherTel', label: '母亲联系方式',  isDecrypt: true, width: 130},
    { prop: 'address', label: '家庭住址', showOverflowTooltip: true  },
    {
      prop: 'operate',
      label: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      list: [
        {
          is: 'currency-button',
          bind: {
            color: '#178782',
            link: true,
            title: '查看',
          },
          on: {
            click: async (rows: KeysProps) => {
              console.log('== 查看 1 ', rows)
              try {
                // const res = await getStudentDetail({ id: rows.id })
                refs.studentDetailPageRef.getParams({
                  data: {
                      "id": 39059,
                      "createTime": "2025-08-19 16:31:16",
                      "updateTime": "2025-08-19 16:31:16",
                      "createId": 27235,
                      "updateId": 27235,
                      "delFlag": 0,
                      "deptId": 10102,
                      "username": "202510288010159",
                      "password": "$2a$10$T5gGqXjefSStzI0eL3atQ.Nu3POkmvGlm79AizF7GqSAV4zgk4zey",
                      "phone": "159****2238_6986",
                      "nickname": "徐**",
                      "email": null,
                      "sex": 2,
                      "avatar": "https://minio.zjjhkm.edu.cn/cdp-public/common/user_avatar.png",
                      "photo": null,
                      "enable": "启用",
                      "remark": null,
                      "deviceToken": "AlXQvzt5dWYV2gehXHgcObBQRrdcEUq7XNpq7jfASBmo",
                      "hasAuth": true,
                      "baiduFace": true,
                      "hkFace": true,
                      "pwdChange": true,
                      "gradeId": 4,
                      "classId": 155,
                      "majorId": 65,
                      "campusId": 1,
                      "buildId": 6,
                      "roomId": 1028,
                      "nation": "汉",
                      "hkImage": null,
                      "hkId": null,
                      "feedbackReadFlag": false,
                      "appCommFlag": true,
                      "accid": "km202510288010159test",
                      "yzjOpenId": null,
                      "leniuSyncTime": "2025-09-15 10:17:35",
                      "leniuUserInfoMd5": "27185f2297de56282771bb67ba136942",
                      "leniuFace": null,
                      "status": "在籍",
                      "polityIdentity": "团员",
                      "belief": "",
                      "fatherTel": "15575382659",
                      "motherTel": "17561202703",
                      "address": "浙江省兰溪市****",
                      "practice": "否",
                      "deptName": "理学院(测试)",
                      "gradeName": "2025级",
                      "className": "生物1班(普高)",
                      "majorName": "生物专业",
                      "campusName": "本部校区",
                      "buildName": "本部校区6号寝室楼623",
                      "chamberName": "623",
                      "headTeacherName": "z教务处01",
                      "teacherNames": [
                          "z教务处01"
                      ],
                      "birthDate": "2025-07-06",
                      "sexName": null,
                      "isPsychological": null,
                      "accidToken": null,
                      "canteenBalance": null,
                      "cardBalance": null
                  }
                })
              } catch (error) {
                console.error(error, '学生查看按钮出错');
              }
            }
          },
        },
        {
          is: 'currency-button',
          bind: {
            color: '#178782',
            link: true,
            title: '修改',
          },
          on: {
            click: (e: any, rows: KeysProps) => {
              console.log('== 修改 11 ', rows)
              router.push({
                name: 'editStudentPage',
                state: {
                  params: {
                    action: 'edit',
                    title: '编辑学生信息',
                    content: '',
                    id: + rows.id
                  }
                }
              })
            }
          },
        },
        {
        is: 'currency-button',
        bind: {
            color: '#178782',
            link: true,
            title: '密码重置',
        },
        on: {
            click: (rows: KeysProps) => {
              useMessageBox('提示', '是否重置密码?')
              // elMessageBox({
              //   message: '是否重置密码?',
              //   callback: async (confirm: string) => {
              //     if (confirm === 'confirm') {
              //         const res = await resetPwd({ id: rows.id })
              //         if (res.code === 200) {
              //           message(res)
              //           await query()
              //         }
              //     }
              //   }
              // })
            },
        },
        },
      ],
    },
])
params.enable = true
// const request = async (params: KeysProps) => await getStudentList(params)
const request = []

export {
    items,
    btns,
    columns,
    params,
    request,
    query,
    activeName,
    editActiveName
 }